package cn.sdfykjyxgs.synclient;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.sdfykjyxgs.synclient.base.HttpResult;
import cn.sdfykjyxgs.synclient.config.HttpTool;
import cn.sdfykjyxgs.synclient.entity.ClientFileChangeLogError;
import cn.sdfykjyxgs.synclient.entity.FileSynConfig;
import cn.sdfykjyxgs.synclient.mapper.ClientFileChangeLogErrorMapper;
import cn.sdfykjyxgs.synclient.server.FileServer;
import cn.sdfykjyxgs.synclient.server.FileSynConfigService;
import cn.sdfykjyxgs.synclient.util.MyFileLogQuery;
import cn.sdfykjyxgs.syncommon.entity.FileChangeLog;
import cn.sdfykjyxgs.syncommon.service.FileChangeLogService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Date;

@MapperScan("cn.sdfykjyxgs.syncommon.mapper")
@MapperScan("cn.sdfykjyxgs.synclient.mapper")
@SpringBootApplication(scanBasePackages = {"cn.sdfykjyxgs"})
public class FileSynClientApplication {

    @Value("${syn.max-retry-count}")
    private long maxRetryCount;

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext app = SpringApplication.run(FileSynClientApplication.class, args);
        //启动成功后初始化文件同步参数
        ApplicationContext context = SpringUtil.getApplicationContext();
        FileSynConfigService bean = context.getBean(FileSynConfigService.class);
        FileServer fileServer = context.getBean(FileServer.class);
        FileChangeLogService logService = context.getBean(FileChangeLogService.class);
        ClientFileChangeLogErrorMapper errorMapper = context.getBean(ClientFileChangeLogErrorMapper.class);
        HttpTool httpTool = context.getBean(HttpTool.class);
        HttpResult httpResult = bean.configPageData();
        FileSynConfig config = (FileSynConfig) httpResult.getData();
        if (config != null) {
            HttpTool.initSetting(config);
            httpTool.changeSpeed(config);
            //单独创建线程进行操作接收到的实时数据
            Thread thread = new Thread() {
                @Override
                public void run() {
                    while (true) {
//                        FileChangeLog take = MyFileLogQuery.take();
                        FileChangeLog take = MyFileLogQuery.get();
                        if (take.getRetryCount() >= 5) {
                            MyFileLogQuery.take();
                            continue;
                        }
                        try {
                            fileServer.synFile(take.getId().longValue());
//                            System.out.println("消费数据：" + take);
                            //入库
                            take.setTime(new Date());
                            logService.save(take);
                            MyFileLogQuery.getRemove();
                        } catch (Exception e) {
                            e.printStackTrace();
                            System.err.println("同步文件遇到错误 尝试日志重新加入队列：" + take);
                            take.setRetryCount(take.getRetryCount() + 1);
                            FileChangeLog temp = BeanUtil.toBean(take, FileChangeLog.class);
                            MyFileLogQuery.getRemove();
                            new Thread(()->{
                                try {
                                    Thread.sleep(100);
                                    MyFileLogQuery.list.add(temp);
                                } catch (InterruptedException ex) {
                                    ex.printStackTrace();
                                }
                            }).start();
//                            MyFileLogQuery.list.set()
                            ClientFileChangeLogError fileChangeLogError = BeanUtil.toBean(take, ClientFileChangeLogError.class);
                            fileChangeLogError.setRetryCount(take.getRetryCount());
                            errorMapper.insert(fileChangeLogError);
//                            MyFileLogQuery.put(take);

                        }


                    }
                }
            };
            thread.start();
        }

    }

}
